package com.nidu.demo.permission.config;

import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.nidu.demo.permission.aop.DataPermissionAspect;
import com.nidu.demo.permission.handler.MybatisDataPermissionHandler;
import com.nidu.demo.permission.service.DataPermissionService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 数据权限自动配置
 * 参考租户系统的TenantAutoConfiguration实现
 */
@Configuration
@EnableConfigurationProperties(DataPermissionProperties.class)
@ConditionalOnProperty(prefix = "cola.data-permission", name = "enabled", havingValue = "true", matchIfMissing = true)
public class DataPermissionAutoConfiguration {

    @Bean
    public DataPermissionInterceptor dataPermissionInterceptor(DataPermissionProperties properties) {
        MybatisDataPermissionHandler handler = new MybatisDataPermissionHandler(properties);
        return new DataPermissionInterceptor(handler);
    }

    @Bean
    public DataPermissionAspect dataPermissionAspect(DataPermissionService dataPermissionService) {
        return new DataPermissionAspect(dataPermissionService);
    }
}